package org.marketing.common.mq;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class PropertiesUtil {
	private  Logger logger = LoggerFactory.getLogger(this.getClass());
    private static PropertiesUtil propsUtil;
    private static PropertiesUtil systemProper;
    private static PropertiesUtil apiAuthProper;
    private static PropertiesUtil idealerProper;
    private static PropertiesUtil rabbitMQProper;
    private static PropertiesUtil chatProper;
    private Properties props = new Properties();
    private String url;
    private PropertiesUtil(String filePath){
        InputStream in = null;
        url=filePath;
        try{
            in = new BufferedInputStream(getClass().getResourceAsStream(filePath));
            props.load(in);
        }catch(Exception e){
        	logger.error(e.getMessage(), e);
        }finally{
            if(in != null){
                try {
                    in.close();
                } catch (IOException e) {
                	logger.error(e.getMessage(), e);
                }
            }
        }
    }
    
    public static PropertiesUtil getInstance(){
        return propsUtil == null?(propsUtil = new PropertiesUtil("/msg_zh.properties")):propsUtil;
    }
    
    public static PropertiesUtil getSysInstance(){
        return systemProper == null?(systemProper = new PropertiesUtil("/sys_msg_zh.properties")):systemProper;
    }
    
    public static PropertiesUtil getApiInstance(){
        return apiAuthProper == null?(apiAuthProper = new PropertiesUtil("/api_auth.properties")):apiAuthProper;
    }
    
    public static PropertiesUtil getIdealerInstance(){
        return idealerProper == null?(idealerProper = new PropertiesUtil("/idealerConfig.properties")):idealerProper;
    }
    
    public static PropertiesUtil getRabbitMQInstance(){
        return rabbitMQProper == null?(rabbitMQProper = new PropertiesUtil("/rabbitMQConfig.properties")):rabbitMQProper;
    }
    
    public static PropertiesUtil getChatInstance(){
        return chatProper == null?(chatProper = new PropertiesUtil("/chatConfig.properties")):chatProper;
    }

	// 根据key读取value
	public String readValue(String key) {
	    return props.getProperty(key);
	}
	
	public void writeProper(String key, String value) throws IOException { 
		OutputStream fos = new FileOutputStream(new File(url)); 
        props.setProperty(key, value); 
        // 将此 Properties 表中的属性列表（键和元素对）写入输出流 
        props.store(fos, "『comments』Update key：" + key); 
    }     
}
